package com.answer.controller.system;

import com.answer.dto.ReturnError;
import com.answer.enums.ReturnInfoEnum;
import com.answer.exception.ElementEmptyException;
import com.answer.exception.RecordEmptyException;
import com.answer.model.Answer;
import com.answer.service.AnswerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/13.
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {
    private static final Logger log = LoggerFactory.getLogger(QuestionController.class);
    @Autowired
    private AnswerService answerService;

    /**
     * 答案选项列表
     *
     * @param key
     * @param pageIndex
     * @param limit
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/listAns", method = RequestMethod.POST)
    public ReturnError listAns(@RequestParam(name = "key", required = false) String key,
                               @RequestParam(name = "qid", required = true) Integer qid,
                               @RequestParam(name = "pageIndex", required = true, defaultValue = "0") Integer pageIndex,
                               @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit,
                               HttpServletRequest request, HttpServletResponse response) {
        List<Answer> answerList = new ArrayList<Answer>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("qId", qid);
            map.put("delFlag", 1);
            PageHelper.startPage(pageIndex + 1, limit);
            answerList = answerService.listByPage(map);
            PageInfo<Answer> p = new PageInfo<Answer>(answerList);
            return new ReturnError(false, null, p.getTotal(), p.getList());
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getInfo(), 0L, null);
        }
    }

    @RequestMapping(value = "/{qid}/saveAns", method = RequestMethod.POST)
    public ReturnError saveAns(@PathVariable("qid") Long id, Answer answer) {
        try {
            return answerService.saveAns(id, answer);
        } catch (ElementEmptyException e1) {
            log.info(ExceptionUtils.getStackTrace(e1));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e1.getMessage(), null);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    @RequestMapping(value = "/updateAns", method = RequestMethod.POST)
    public ReturnError updateAns(Answer answer) {
        try {
            return answerService.updateAns(answer);
        } catch (ElementEmptyException e1) {
            log.info(ExceptionUtils.getStackTrace(e1));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e1.getMessage(), null);
        } catch (RecordEmptyException e2) {
            log.info(ExceptionUtils.getStackTrace(e2));
            return new ReturnError(true, ReturnInfoEnum.EMPTY.getType(), e2.getMessage(), null);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ReturnError removes(String ids) {
        try {
            return answerService.removes(ids);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo(), null);
        }
    }
}
